<?php
class Services extends Controller {
	
	function Services()
	{
		parent::Controller();
		$this->_container = $this->config->item('FAL_template_dir').'template_admin/container';
		$this->load->library('CNC_services', 'cnc_services');
		$this->freakauth_light->check();
	}
	
	function index()
	{
		
	}
	function grocery()
	{
		$data['Cnc'] = $this->cnc_services->grocery($this->uri->segment(4));
		$this->load->view($this->_container,$data);
	}

	function addGrocery()
	{
		$this->load->model('Services_model','services_model');
		$values = $this->getFormValues();
		$this->services_model->addGrocery($values);
	    $query = $this->services_model->getGrocery($this->input->get('userid'));
	    $data=array();
        if ($query->num_rows()>0)
		{ 
			 $data['grocery']=$query->result();
		}
		$data['action']="Manage Grocery";
    	$data['controller']="admin/services";
    	$data['id'] = $this->input->get('userid');
        $data['heading'] = "Grocery";
		$this->load->view($this->config->item('FAL_template_dir').'template_admin/services/grocery_ajax',$data);
				
	}
	function getFormValues()
	{
	$this->load->helper('date');
    $values['userid'] =$this->input->get('userid');
	$values['name']	=$this->input->get('name');
	$values['brand'] =$this->input->get('brand');
	$values['quantity'] =$this->input->get('quantity');
	$values['date'] =$this->input->get('date');  
	$values['schedule'] =$this->input->get('schedule');
	$values['ordered_date'] = now();
	return $values;
	
	}
	
	function editGrocery()
	{
		$this->load->model('Services_model','services_model');
		$id=$this->input->get('id');
		$values= $this->getFormValues();
		$this->services_model->editGrocery($id,$values);
	    $query = $this->services_model->getEditedRow($id);
        if ($query->num_rows()>0)
		{ 
			 $grocery =$query->row();
		}	
     //returning in xml format
     header('Content-Type:text/xml');
      echo "<?xml version='1.0' encoding='UTF-8'?>\n";
     
     echo "<reply><item><name>".$grocery->name."</name><id>".$grocery->id."</id><brand>".$grocery->brand."
     </brand><quantity>".$grocery->quantity."</quantity><date>".$grocery->date."</date>
     <schedule>".$grocery->schedule."</schedule></item></reply>";
     
     //$this->load->view($this->config->item('FAL_template_dir').'template_admin/services/grocery_test',$data);
	   
	}
	function deleteGrocery()
	{
		$this->load->model('Services_model','services_model');
		$this->services_model->deleteGrocery($this->input->get('id'));
		echo "hello";
  
	}
	
}
?>